using AutojsController.Services;
using AutojsController.ViewModels;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Avalonia.Platform.Storage;
using Avalonia.VisualTree;
using System.Linq;
using System.Threading.Tasks;

namespace AutojsController.Views;

public partial class ScriptView : UserControl
{
    public ScriptView()
    {
        InitializeComponent();
    }

    protected override async void OnPointerPressed(PointerPressedEventArgs e)
    {
        base.OnPointerPressed(e);
        var point = e.GetCurrentPoint(this);
        // 查找当前点击位置是否有脚本组件
        ScriptDataModel? scriptDM = null;
        var hitTest = this.InputHitTest(point.Position);
        var current = hitTest as Visual;

        while (current != null)
        {
            if (current is Border border && border.DataContext is ScriptDataModel)
            {
                scriptDM = border.DataContext as ScriptDataModel;
                break;
            }
            current = current.GetVisualParent();
        }
        // 设置脚本组件设置状态
        var scriptVM = (ScriptViewModel)this.DataContext!;
        if (scriptDM != null)
        {
            if (point.Properties.IsLeftButtonPressed)
                scriptVM.SelectScriptOrNot(scriptDM);
            else if (point.Properties.IsRightButtonPressed)
                scriptVM.SetScriptSelected(scriptDM);
        }
        // 调整菜单
        if (point.Properties.IsRightButtonPressed)
        {
            ImportMenuItem.CommandParameter = TopLevel.GetTopLevel(this)?.StorageProvider;
            var selectedSrp = scriptVM.Scripts.FirstOrDefault(x => x.IsSelected);
            if (selectedSrp != null)
            {
                RemoveMenuItem.IsVisible = true;
                if (selectedSrp.Script.Validated)
                {
                    ValidateMenuItem.IsVisible = false;
                    AssignToGroupMenuItem.IsVisible = true;
                    AssignToGroupMenuItem.Items.Clear();
                    var grpService = (Application.Current as App)!.GetRequiredService<GroupsService>() as GroupsService;
                    foreach (var grp in await grpService.getAllGroupsAsync())
                    {
                        MenuItem mi = new MenuItem();
                        mi.Header = grp.Name;
                        mi.Command = scriptVM.AssignToGroupCommand;
                        mi.CommandParameter = grp.Id;
                        if (selectedSrp.Script.Id == grp.ScriptID) mi.IsEnabled = false;
                        AssignToGroupMenuItem.Items.Add(mi);
                    }
                }
                else
                {
                    AssignToGroupMenuItem.IsVisible = false;
                    ValidateMenuItem.Items.Clear();
                    ValidateMenuItem.IsVisible = true;
                    var mobService = (Application.Current as App)!.GetRequiredService<MobilesService>() as MobilesService;
                    foreach (var mob in await mobService.GetRandomAvalibleMobilesAsync(3))
                    {
                        MenuItem mi = new MenuItem();
                        mi.Header = mob.Address;
                        mi.Command = scriptVM.ValidateCommand;
                        mi.CommandParameter = mob.Id;
                        ValidateMenuItem.Items.Add(mi);
                    }
                }
            }
            else
            {
                ValidateMenuItem.Items.Clear();
                ValidateMenuItem.IsVisible = false;
                RemoveMenuItem.IsVisible = false;
                AssignToGroupMenuItem.Items.Clear();
                AssignToGroupMenuItem.IsVisible = false;
            }
        }
    }
}